package com.example.a79273.shandianban.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.adapter.YouRenRecyclerViewAdapter;
import com.example.a79273.shandianban.application.MyApplication;
import com.example.a79273.shandianban.config.NetConfigYouRen;
import com.example.a79273.shandianban.model.YouRenPageBean;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 有人页面中下面的ViewPager中的页卡
 * 页卡中应用RecyclerView
 */
public class YouRenPageFragment extends Fragment {

    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private YouRenRecyclerViewAdapter mAdapter;
    private List<YouRenPageBean.DataBean.ListBean> mData;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceSate) {
        View view = inflater.inflate(R.layout.fragment_yourenpage, null);
        initView(view);
        initData();
        setData();
        return view;
    }

    private void setData() {
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(mAdapter);
    }

    private void initData() {
        mData = new ArrayList<>();
        Bundle bundle = getArguments();

        if (bundle != null) {
            int location = bundle.getInt("location");
            String url = null;
            switch (location) {
                case 0:
                    url = NetConfigYouRen.LATEST_URL;
                    break;
                case 1:
                    url = NetConfigYouRen.Hot_URL;
                    break;
                case 2:
                    url = NetConfigYouRen.FEMALE_URL;
                    break;
                case 3:
                    url = NetConfigYouRen.OTHERS_URL;
                    break;
                default:
                    break;

            }
            if (!TextUtils.isEmpty(url)) {

                x.http().get(new RequestParams(url), new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        if (!TextUtils.isEmpty(result)) {
                            YouRenPageBean pageBean = YouRenPageBean.objectFromData(MyApplication.dealWithString(result));
                            if (pageBean != null) {
                                mData.addAll(pageBean.getData().getList());
                                mAdapter.notifyDataSetChanged();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {

                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
            }
            layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        }

        mAdapter = new YouRenRecyclerViewAdapter(mData, getActivity());

    }

    private void initView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_page_fragment);
    }
}
